//
//  QImage.m
//  ImageEffect
//
//  Created by lichq on 14-1-17.
//  Copyright (c) 2014年 lichq. All rights reserved.
//

#import "QImage.h"

@implementation QImage

CGContextRef CreateRGBABitmapContext (CGImageRef inImage)// √ã√∏√Æ√Ç√µ√ª‚Ä∞‚àè√Ñ‚Ä∞‚àè‚Ñ¢‚Ä∞Œ©√∏√Å√Æ¬ÆRGBA√à√Ñ√∂√à√Ö√¨√Å√∂√ë‚Ä∞Œ©√ß√Ç√µ√¶‚Ä∞‚àè√§‚Ä∞‚àè√£√ä√±√°
{
	CGContextRef context = NULL;
	CGColorSpaceRef colorSpace;
	void *bitmapData; //√Ç√ú√ñ√Ç‚â†√≤√Å¬©‚à´√à√≥¬•√Å√∂√ë√ä√•√°√à√≠√†√î¬∫√•√ã√ò‚Ä¢√Ç√ú√ñ√Ç‚â†√≤√Å¬©‚à´√à√≥¬•√Å√∂√ë√Ç¬ß√ü√Ç‚àû√®√Å‚â†√¢‚Ä∞‚à´√©√Ç√µ√¶√Ç√â√®‚Ä∞Œ©√∏√Å√Æ¬ÆRGB√à√Ñ√∂√à√Ö√¨√ä√¢√Ñ√Ç√ß‚Ä†√Å√Æ¬Æ√Å√∂√ë√Ç‚â†√≥√ã√§√á√ä√Ø‚àû‚Äû√Ñ√?
	int bitmapByteCount;
	int bitmapBytesPerRow;
    
	size_t pixelsWide = CGImageGetWidth(inImage); //√ã√©‚àë√Ç√®√±√ä¬Æ‚Ñ¢√Ç√™√´√Å√∂√ë√Ç√â√®√Å¬•‚Ä†√Å√áœÄ√Å√∂√ë‚Ä∞‚àè‚Ñ¢√ä√Ø‚àû
	size_t pixelsHigh = CGImageGetHeight(inImage); //√Å‚à´¬µ√Ç√™√?
    
	bitmapBytesPerRow	= (pixelsWide * 4); //√ä√ò√®‚Ä∞‚àè√Ñ√ã¬∞√•√Å√∂√ë√Ç√â√®√Å¬•‚Ä†√Å√áœÄ√Ç√ß‚Ä†√Å√Æ¬Æ√Å√∂√ë√Ç‚â†√≥√ã√§√á√ä√Ø‚àû√î¬∫√•√ä√ò√®‚Ä∞‚àè‚Ñ¢√Ç√â√®√Å¬•‚Ä†√Å√áœÄ√Å√∂√ëARGB√Ç√µ√µ‚Ä∞‚àè‚Ñ¢√à√Ñ√∂√à√Ö√¨√Ç√™√ë√Ç√ß‚Ä?‚Ä∞‚àè‚Ñ¢bit(0-255)√Å√∂√ë√Å¬©‚à´√à√≥¬?
	bitmapByteCount	= (bitmapBytesPerRow * pixelsHigh); //√ã√Ü¬∞√Å√Ü√≥√ä√Ø¬•√Ç¬∫‚Ä†√Ç√µ√¶√Ç√ß‚Ä†√Å√Æ¬Æ√Å√∂√ë√Ç‚â†√≥√ã√§√á√ä√Ø‚à?
    
	colorSpace = CGColorSpaceCreateDeviceRGB();//√Ç√†√µ√Ç¬™‚à´‚Ä∞√¶√π√ã¬µ√±‚Ä∞‚à´√©√ã√Ü√¶√Ç¬ß√°√Å√∂√ëRGB√à√Ñ√∂√à√Ö√¨
	
	bitmapData = malloc(bitmapByteCount); //√Ç√†√ú√à√ñ√ß√ã‚àÇ‚â•√Ç¬ß√º√Ç√ÜœÄ√Å‚à´‚â•√Ç√µ√¶√Å√¢√°√Ç‚â†√≥√ã√§√á√ä√Ø‚àû√Å√∂√ë√Ç√ú√ñ√Ç‚â†√≤√Å¬©‚à´√à√≥¬•
    
	context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
    //√Ç√†√µ√Ç¬™‚à´CoreGraphic√Å√∂√ë√Ç√µ√¶√ÇŒ©¬¢‚Ä∞‚àè√§‚Ä∞‚àè√£√ä√±√°√î¬∫√•√ã√ò‚Ä¢‚Ä∞‚àè√§‚Ä∞‚àè√£√ä√±√°√ä√®√®√ã√∏‚àû‚Ä∞‚à´√úbitmaData√ä√•√°√Ç√™√´√Å√∂√ë√Ç√ú√ñ√Ç‚â†√≤√Å¬©‚à´√à√≥¬•√à√∫√Ñ√ã¬∂√Ö√Å¬™√≤√Ç√†‚àÇ√Å√∂√ë√Ç√µ√¶√Ç√â√®√Å√∂√ë‚Ä∞‚àè√Ñ‚Ä∞‚à´√µ√Å¬™√≤√Ç√†‚àÇ√Ç√®√á√ä√Ø‚àû
    
	CGColorSpaceRelease( colorSpace );
    //Core Foundation‚Ä∞‚àè‚â†√à√Ñ√∂√ã√∏√°√Ç√™¬¥√ä√∫√¢Create‚Äû√Ñ√ÖAlloc√Å√∂√ë√ä√±œÄ√ä‚â•√Ø√Ç√™√ß√Ç‚â†√≥√Ç√†√µ√Ç¬™‚à´√Å√∂√ë√ä√•√°√à√≠√†√î¬∫√•√à√∫√Ñ√ã¬∂√Ö‚Ä∞Œ©√∏√Å√Æ¬ÆCFRelease()√Ç√°Œ©√ä√Ø‚àû√à√°√§√ä√Æ√?
    
	return context;
}

unsigned char *RequestImagePixelData(UIImage *inImage)
// √ã√∏√Æ√Ç√µ√ª‚Ä∞‚àè√Ñ‚Ä∞‚àè‚Ñ¢√ä√•√°√à√≠√†√î¬∫√•√ã√ò‚Ä¢√ä√•√°√à√≠√†√ä√•√°√Ç√™√´‚Ä∞‚àè√Ñ‚Ä∞‚àè‚Ñ¢√ä√Ø‚àû√Å¬™√ë√î¬∫√•√ä√Ø‚àû√Å¬™√ë‚Ä∞‚àè‚â†√Å√∂√ë√ä√ò√®√Ç√µ√µ‚Ä∞‚àè‚Ñ¢√Ç√ñ√â√Å¬•‚Ä†√à√âŒ©√ä√≤√ò√Ç√µ√¶√Ç√â√®‚Ä∞‚àè√§√Å√∂√ë‚Ä∞‚àè√Ñ‚Ä∞‚àè‚Ñ¢√Ç√â√®√Å¬•‚Ä†√Å√áœÄ√Å√∂√ëRGBA√Å√∂√ë√ä√Ø‚àû√Ç√Ñ¬?0-255)√î¬∫√•√Å√Æ¬Æ√ä√≥‚Ä†√Å¬®¬∂√Ç√®‚àë√Å√∂√ëchar√ä√≤√ò√Ç√µ‚Ä†‚Ä∞‚àè‚à´√Ç√Ü√â√ä‚â†¬£√Ç‚Ä¢Œ©√Å√∂√ë√Ç√®√±√Ç√Ñ¬∫√ã√•√â√Ç√µ¬•√Ç‚àû¬±√ä√≤√ò0-255
{
	CGImageRef img = [inImage CGImage];
	CGSize size = [inImage size];
    
	CGContextRef cgctx = CreateRGBABitmapContext(img); //‚Ä∞Œ©√∏√Å√Æ¬Æ‚Ä∞‚àè√§√à√π¬¢√Å√∂√ë√Ç√°Œ©√ä√Ø‚àû√Ç√†√µ√Ç¬™‚à´‚Ä∞‚àè√§‚Ä∞‚àè√£√ä√±√°
	
	CGRect rect = {{0,0},{size.width, size.height}};
    
	CGContextDrawImage(cgctx, rect, img); //√Ç‚àû√ú√Å√µ√Ü√ä‚Ä†√°√Ç√µ√¶√Ç√â√®√Å¬™√≤√Ç√†‚àÇ√Ç√†‚àû√ä√•√°√Ç√Ü√∂√Å√∂√ë‚Ä∞‚àè√§‚Ä∞‚àè√£√ä√±√°√î¬∫√•√Ç√Ü√ª√à√¥√ñ‚Ä∞‚àè‚à´‚Ä∞‚àè√§‚Ä∞‚àè√£√ä√±√°√Ç√ú√ñ√Å√∂√ëbitmapData‚Äû√Ñ√?
	unsigned char *data = CGBitmapContextGetData (cgctx);
    
	CGContextRelease(cgctx);//√à√°√§√ä√Æ√¶‚Ä∞‚àè√§√à√π¬¢√Å√∂√ë√Ç√°Œ©√ä√Ø‚àû√Ç√†√µ√Ç¬™‚à´√Å√∂√ë‚Ä∞‚àè√§‚Ä∞‚àè√£√ä√±√°
	return data;
}

+ (UIImage *)createNewImageByImagePixel:(unsigned char *)imagePixel width:(int)w height:(int)h{
    //‚Ä∞‚àè√£√à√π¬¢√Å√∂√ë‚Ä∞¬™¬£√Å‚Ä†√Ö√Ç√†√µ√Ç¬™‚à´√ã¬∂√Ö√ã√¶√¨√Ç√°‚à´√Å√∂√ë√Ç√µ√¶√Ç√â√®√Å√∂√ë√Å√µ‚àè√Ç√ñ‚â•√Ç√®√á√ä√Ø‚àû
    NSInteger dataLength = w * h * 4;
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imagePixel, dataLength, NULL);
    
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * w;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    
    CGImageRef newImageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow,colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);//√Ç√†√µ√Ç¬™‚à´√ã¬∂√Ö√ã√¶√¨√Ç√°‚à´√Å√∂√ë√Ç√µ√¶√Ç√â√®
    
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    
    CFRelease(newImageRef);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);
    
    return newImage;
}



+ (QImagePixelInfo)getImagePixelInfoFromImage:(UIImage *)image{
    unsigned char *imagePixel = RequestImagePixelData(image);
    CGImageRef imageRef = [image CGImage];
	int w = CGImageGetWidth(imageRef);
	int h = CGImageGetHeight(imageRef);
    
    QImagePixelInfo imagePixelInfo;
    imagePixelInfo.imagePixel = imagePixel;
    imagePixelInfo.imageW = w;
    imagePixelInfo.imageH = h;
    
    return imagePixelInfo;
}

@end
